package com.burgeon.backend.Rest.base;

import org.springframework.lang.NonNull;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class sipSign {
    public String timestamp() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(now)+".000";
        return dateString;
    }
    @NonNull
    private String md5(@NonNull String string) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String getMD5Password(String sip_appkey, String sip_timestamp, String appSecret) {
        String passwordMD5 = null;
        passwordMD5 = md5(appSecret);
        String result = md5(sip_appkey + sip_timestamp + passwordMD5);
        return result;
    }

    public static void main(String[] args) {
        sipSign sipSign = new sipSign();
        String data = sipSign.getMD5Password("nea@burgeon.com.cn", sipSign.timestamp(), "123");

    }
}
